package com.stu.student_back_end.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.stu.student_back_end.service.ITblAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Date;

/**
 * 生成token
 */
@Slf4j
@Component
public class TokenUtil {

    private static final long EXPIRE_TIME = 60*60*2*1000;  //过期时间2小时

    private static String token = "";

    public static String getToken() {
        return token;
    }

    public static void setToken(String token) {
        TokenUtil.token = token;
    }

    public static String createToken(String accountId,String sign) {
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
         return JWT.create().withAudience(accountId) //将account的id作为token的载荷
                    .withExpiresAt(date)   //设置过期时间
                    .sign(Algorithm.HMAC256(sign)); //以密码做密钥
    }

    public static boolean verifyToken(String VToken) {
        return VToken.equals(token);
    }
}
